import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state: {
        // 
        car: car//将购物车中的数据用一个数组存起来
    },
    mutations: {
        addToCar(state, goodsinfo) {
            var flag = false

            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })
            // 如果循环完毕得到flag还是false则把商品数据push到购物车中
            if (!flag) {
                state.car.push(goodsinfo)

            }
            // 当更新car之后  存储到本地的localStorage
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updataGoodsInfo(state, goodsinfo) {
            //修改购物车的数量值
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count = parseInt(goodsinfo.count)
                    return true
                }
            })
            // 当修改完之后保存到数组
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeFormCar(state,id) {
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1)
                    return true;
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodSelected(state,info){
            state.car.some(item=>{
                if(item.id==info.id){
                    item.selected=info.selected
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }

    },
    getters: {
        // 相当于计算属性
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count
            })
            return c

        },
        getGoodsCount(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count
            })
            return o
        },
        getGoodSelected(state){
            var o={}
            state.car.forEach(item=>{
                o[item.id]=item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o={
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count+=item.count
                    o.amount+=item.price*item.count
                }
            })
            return o
        }

    }
})

import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import app from './app.vue'
// 导包
import VueRouter from 'vue-router'
import home from './components/tabbar/home.vue'
import member from './components/tabbar/member.vue'
import shop from './components/tabbar/shop.vue'
import search from './components/tabbar/search.vue'
import VueResource from "vue-resource"
import NewsList from "./components/news/newsList.vue"
import Newsinfo from "./components/news/Newsinfo.vue"
import photoist from "./components/photolist/photolist.vue"
import photoinfo from "./components/photolist/photoinfo.vue"
import GoodList from "./components/goods/GoodList.vue"
import goodlistinfo from "./components/goods/goodlistinfo.vue"
import goodsDesc from "./components/goods/goodsDesc.vue"
import goodsComment from "./components/goods/goodsComment.vue"


Vue.use(VueResource)
// 设置请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005'
Vue.http.options.emulateJSON = true;

import moment from "moment"
// 到入bootstrap

import 'bootstrap/dist/css/bootstrap.css'
import './lib/MUI/css/mui.css'
import './lib/MUI/css/icons-extra.css'
import VuePreview from 'vue-preview'

// defalut install
Vue.use(VuePreview)

// with parameters install


// 这是到入MUI的样式表和bootstrap用法没有区别

// Vue.use(MintUI)

// 定义全局的过滤器
Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(pattern)
})






Vue.use(MintUI)
var router = new VueRouter({
    routes: [//配置路由规则
        { path: '/', redirect: '/home' },
        { path: '/home', component: home },
        { path: '/member', component: member },
        { path: '/shop', component: shop },
        { path: '/search', component: search },
        { path: '/home/newsList', component: NewsList },
        { path: '/home/newsinfo/:id', component: Newsinfo },
        { path: '/home/photolist', component: photoist },
        { path: '/home/photoinfo:id', component: photoinfo },
        { path: '/home/goodslist', component: GoodList },
        { path: '/home/goodlistinfo/:id', component: goodlistinfo, name: 'goodlistinfo' },
        { path: '/home/goodsdesc/:id', component: goodsDesc, name: 'goodsdesc' },
        { path: '/home/goodsComment/:id', component: goodsComment, name: 'goodsComment' },

    ],
    linkActiveClass: 'mui-active'
    // 默认类   router-link-active
})



// 安装
Vue.use(VueRouter)



var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,//将路由对象挂在到vm上
    store
})




